package com.kkd.kkdapp.http.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import android.text.TextUtils;

import com.kkd.kkdapp.pinnedsection.TimeManagement;

public class DealDetailItemBean {
	// 类型--内容
	public static final int ITEM = 0;
	// 类型--顶部悬浮的标题
	public static final int SECTION = 1;
	// 所属于的类型
	public final int type;
	// listview显示的item的数据实体类,可根据自己的项目来设置
	public final DealDetailItem dealDetailItem;

	public int sectionPosition; // 顶部悬浮的标题的位置
	public int listPosition; // 内容的位置

	public int getSectionPosition() {
		return sectionPosition;
	}

	public void setSectionPosition(int sectionPosition) {
		this.sectionPosition = sectionPosition;
	}

	public DealDetailItem getDealDetailItem() {
		return dealDetailItem;
	}

	public int getListPosition() {
		return listPosition;
	}

	public void setListPosition(int listPosition) {
		this.listPosition = listPosition;
	}

	public int getType() {
		return type;
	}

	public DealDetailItemBean(int type, DealDetailItem dealDetailItem) {
		super();
		this.type = type;
		this.dealDetailItem = dealDetailItem;
	}

	public DealDetailItemBean(int type, DealDetailItem dealDetailItem,
			int sectionPosition, int listPosition) {
		super();
		this.type = type;
		this.dealDetailItem = dealDetailItem;
		this.sectionPosition = sectionPosition;
		this.listPosition = listPosition;
	}

	@Override
	public String toString() {
		return dealDetailItem.getCreateTime() + "===" + type;
	}

	public static ArrayList<DealDetailItemBean> getData(
			List<DealDetailItem> details) {
		// 最后我们要返回带有分组的list,初始化
		ArrayList<DealDetailItemBean> list = new ArrayList<DealDetailItemBean>();
		// 时间转换的util类
		TimeManagement management = new TimeManagement();
		Map<DealDetailItem, List<DealDetailItem>> map = new LinkedHashMap<DealDetailItem, List<DealDetailItem>>();
		DealDetailItem detail = new DealDetailItem();
		// 按照details里面的时间进行分类
		for (int i = 0; i < details.size(); i++) {
			try {
				String key = management.exchangeStringDate(details.get(i)
						.getCreateTime());
				if (!TextUtils.isEmpty(detail.getCreateTime())) {
					// 判断这个Key对象有没有生成,保证是唯一对象.如果第一次没有生成,那么new一个对象,之后同组的其他item都指向这个key
					boolean b = !key.equals(detail.getCreateTime().toString());
					if (b) {
						detail = new DealDetailItem();
					}
				}
				detail.setCreateTime(key);
				// 把属于当月yyyy-MM-dd的时间HH:mm:ss全部指向这个key
				List<DealDetailItem> warnDetails = map.get(detail);
				// 判断这个key对应的值有没有初始化,若第一次进来,这new一个arryalist对象,之后属于这一天的item都加到这个集合里面
				if (warnDetails == null) {
					warnDetails = new ArrayList<DealDetailItem>();
				}
				warnDetails.add(details.get(i));
				map.put(detail, warnDetails);
			} catch (Exception e) {

			}
		}
		// 用迭代器遍历map添加到list里面
		Iterator iterator = map.entrySet().iterator();
		while (iterator.hasNext()) {
			Map.Entry entry = (Entry) iterator.next();
			DealDetailItem key = (DealDetailItem) entry.getKey();
			// key(yyyy-MM-dd)作为标题.类别属于SECTION
			list.add(new DealDetailItemBean(SECTION, key));
			List<DealDetailItem> li = (List<DealDetailItem>) entry.getValue();
			for (DealDetailItem warnDetail : li) {
				// 对应的值(HH:mm:ss)作为标题下的item,类别属于ITEM
				list.add(new DealDetailItemBean(ITEM, warnDetail));
			}
		}
		// 把分好类的hashmap添加到list里面便于显示
		return list;
	}
}
